layui.define(['form'],function (exports) {
    var form = layui.form;
    var controller = {
        login:function () {
            form.render();
            $(document.body).keyup(function(e){
                if(e.keyCode === 13){
                    $("button[lay-filter=LAY-user-login-submit]").click();
                    return false;
                }
            });

            //提交
            form.on('submit(LAY-user-login-submit)', function(obj){
                //请求登入接口
                let load = layer.load();
                $.post(Api.login,obj.field)
                    .success(function (res) {
                        if(res.code){
                            //登入成功的提示与跳转
                            layer.msg(res.msg, {
                                offset: '15px'
                                ,icon: 1
                                ,time: 1000
                            }, function(){
                                location.href = res.url; //后台主页
                            });
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    })
                    .error(function (res) {
                        notice.error('服务器异常');
                    })
                    .complete(function () {
                        $("#change_captcha").get(0).click();
                        layer.close(load);
                    })
            });
        }
    };
    exports('admin_login',controller);
});